package com.ws.service.impl;

import com.github.pagehelper.Page;
import com.ws.common.CommonConstant;
import com.ws.common.PaasPage;
import com.ws.entity.Reader;
import com.ws.repository.ReaderRepository;
import com.ws.service.ReaderService;
import com.ws.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ：ws
 * @date ：Created in 2023/10/24 12:13
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Service
public class ReaderServiceImpl implements ReaderService {
    @Autowired
    ReaderRepository readerRepository;

    @Override
    public PaasPage<Reader> getReaderPage(Pageable pageable, String readerName, String gender,String statusCd) {
        PageUtil.setPageAndOrderBy(pageable);
        Reader reader = new Reader();
        reader.setReaderName(readerName);
        reader.setGender(gender);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        reader.setStatusCd(statusCd);
        Page<Reader> readerPage = readerRepository.getReaderPage(reader);
        return PageUtil.transformPage(readerPage);
    }

    @Override
    public Reader saveReader(Reader reader) {
        reader.setCreateTime(new Date());
        reader.setStatusCd(CommonConstant.ADD_STATUS_CD);
        readerRepository.saveReader(reader);
        return reader;
    }

    @Override
    public Reader getReaderBasicInfo(Long readerId) {
        return readerRepository.getReaderBasicInfo(readerId);
    }

    @Override
    public void delReader(Long readerId) {
        readerRepository.delReader(readerId);
    }

    @Override
    public Reader updateReader(Long readerId, Reader reader) {
        readerRepository.updateReader(reader);
        return reader;
    }

    @Override
    public List<Reader> getReaderList(String readerName, String gender, String statusCd) {
        Reader reader = new Reader();
        reader.setReaderName(readerName);
        reader.setGender(gender);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        reader.setStatusCd(statusCd);
        List<Reader> readerList = readerRepository.getReaderList(reader);
        return readerList;
    }
}
